package com.dhcc.sds.standard.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.dao.CdcExamdicMapper;
import com.dhcc.cdc.busi.dao.CdcLabItemMapper;
import com.dhcc.cdc.busi.dao.CdcLabOrderMapper;
import com.dhcc.cdc.busi.dao.CdcOrdmastMapper;
import com.dhcc.cdc.busi.entity.CdcLabItemEntity;
import com.dhcc.cdc.busi.entity.CdcLabOrderEntity;
import com.dhcc.cdc.busi.entity.CdcOrdmastEntity;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.sds.standard.dao.SdsStandDicMapper;
import com.dhcc.sds.standard.dao.SdsStandDicsubMapper;
import com.dhcc.sds.standard.dao.SdsStdDicMatchLogMapper;
import com.dhcc.sds.standard.entity.SdsStandDicEntity;
import com.dhcc.sds.standard.entity.SdsStdDicMatchLogEntity;
import com.dhcc.sds.standard.service.ISdsStdDicMatchLogService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 标准化对照日志记录表表数据服务层接口
 * 
 * @ClassName: stdDicMatchLogService
 * @Description: TODO
 * @author zhangdc
 * @date 2021-05-08 16:01:23
 */
@Service("stdDicMatchLogService")
public class SdsStdDicMatchLogServiceImpl extends BaseServiceImpl<SdsStdDicMatchLogMapper, SdsStdDicMatchLogEntity> implements ISdsStdDicMatchLogService {

	@Resource
	private SdsStandDicsubMapper standDicsubMapper;

	@Resource
	private SdsStandDicMapper standDicMapper;

	@Resource
	private CdcOrdmastMapper ordmastMapper;

	@Resource
	private CdcExamdicMapper cdcExamdicMapper;

	@Resource
	private CdcLabOrderMapper cdcLabOrderMapper;

	@Resource
	private CdcLabItemMapper cdcLabitemMapper;

	@Override
	public List<SdsStdDicMatchLogEntity> page(Page<SdsStdDicMatchLogEntity> page, Map<String, Object> map) {
		List<SdsStdDicMatchLogEntity> page1 = this.baseMapper.page(page, map);
		for (SdsStdDicMatchLogEntity sdsStdDicMatchLogEntity : page1){
			Long dicId = sdsStdDicMatchLogEntity.getDicId();
			String matchType = sdsStdDicMatchLogEntity.getMatchType();
			Long ordmastId = sdsStdDicMatchLogEntity.getOrdmastId();
			Integer operType = sdsStdDicMatchLogEntity.getOperType();
			Long laborderId = sdsStdDicMatchLogEntity.getLaborderId();
			Long labitemId = sdsStdDicMatchLogEntity.getLabitemId();
			String operTypeDesc;
			if (operType == 1){
				operTypeDesc = "对照";
			}else{
				operTypeDesc = "撤销";
			}

			//操作内容-医嘱
			if ("1".equals(matchType) && !StringUtils.isEmpty(ordmastId)){
				CdcOrdmastEntity ordmastEntity = ordmastMapper.selectById(ordmastId);
				if (!StringUtils.isEmpty(ordmastEntity)){
					sdsStdDicMatchLogEntity.setOperContent(ordmastEntity.getDescription());
				}
				sdsStdDicMatchLogEntity.setMatchType("对照医嘱");
			}
			//检验医嘱
			if ("2".equals(matchType)){
				if (!StringUtils.isEmpty(dicId)){
					CdcLabOrderEntity cdcLabOrderEntity = cdcLabOrderMapper.selectById(laborderId);
					if (!StringUtils.isEmpty(cdcLabOrderEntity)){
						sdsStdDicMatchLogEntity.setOperContent(cdcLabOrderEntity.getDescription());
					}
				}
				sdsStdDicMatchLogEntity.setMatchType("对照检验医嘱");
			}
			//检验项目
			if("3".equals(matchType)){
				if (!StringUtils.isEmpty(dicId)){
					CdcLabItemEntity cdcLabItemEntity = cdcLabitemMapper.selectById(labitemId);
					if (!StringUtils.isEmpty(cdcLabItemEntity)){
						sdsStdDicMatchLogEntity.setOperContent(cdcLabItemEntity.getDescription());
					}
				}
				sdsStdDicMatchLogEntity.setMatchType("对照检验项目");
			}
			//电子病历子标题
			if("4".equals(matchType)){
				if (!StringUtils.isEmpty(dicId)){
					sdsStdDicMatchLogEntity.setOperContent(sdsStdDicMatchLogEntity.getEmrSubTitle());
				}
				sdsStdDicMatchLogEntity.setMatchType("对照病历子标题");
			}
			if (!StringUtils.isEmpty(dicId)){
				//递归
				sdsStdDicMatchLogEntity.setDicDesc(this.getAllPath(dicId,""));
			}
		}

		//查询过滤
		List<SdsStdDicMatchLogEntity> res = new ArrayList<>();
		String dicDesc = (String) map.get("dicDesc");
		if (!StringUtils.isEmpty(dicDesc)){
			res = page1.stream().filter(i -> i.getDicDesc().contains(dicDesc)).collect(Collectors.toList());
			return res;
		}else {
			return page1;
		}

	}
	private String getAllPath(Long dicId,String res){
		SdsStandDicEntity standDicEntity = standDicMapper.selectById(dicId);
		if(CommonUtil.isNotEmpty(standDicEntity)){
			if(CommonUtil.isEmpty(res)){
				res = standDicEntity.getName();
			}else{
				res = standDicEntity.getName()+"-"+res;
			}

			if(CommonUtil.isNotEmpty(standDicEntity.getParentId())&&0L!=standDicEntity.getParentId()){
				res = this.getAllPath(standDicEntity.getParentId(),res);
			}else{
				return res;
			}
		}
		return res;
	}
	@Override
	public List<SdsStdDicMatchLogEntity> list(Map<String, Object> map) {
		return this.baseMapper.list(map);
	}

}